package online.hxstart.code.concurrent.chapter03;

import java.util.concurrent.TimeUnit;

/**
 * @author huxing
 * @date 19-4-24 星期三 上午10:10
 * @describe
 */
public class FlagThreadExit {

  static class MyTask extends Thread {
    private volatile boolean closed = false;

    @Override
    public void run() {
      System.out.println("I will start work");
      while (!closed && !isInterrupted()) {}

      System.out.println("I will be exiting.");
    }

    public void close() {
      this.closed = false;
      this.interrupt();
    }
  }

  public static void main(String[] args) throws InterruptedException {
    MyTask task = new MyTask();
    task.start();
    TimeUnit.MILLISECONDS.sleep(1);
    System.out.println("System will be shutdown");
    task.close();
  }
}
